<!doctype html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport"
          content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
    <script src="./vue.global.js"></script>
    <style>
        .err {
            color: #f00;
            position: relative;
            top: 10px
        }
        .result {
            font-family: sans-serif;
        }
        textarea {
            border: 1px solid #ccc;
            outline: none;
            width: 500px;
            height: 300px;
            margin-right: 20px;
            background: #f3f3f3;
            padding: 20px;
        }
        textarea:focus {
            border-color: #aaa;
        }
        .result {
            font-family: monospace;
            width: 1102px;
            border: 1px solid #ccc;
            padding: 20px;
            box-sizing: border-box;
            white-space: nowrap;
            overflow: auto;
            line-height: 3;
            color: #8ee2b6;
        }
    </style>
</head>
<body>
<h1>文本对比小工具</h1>
<div id="app">
    <textarea v-model="t1" cols="30" rows="10" placeholder="文本1"></textarea>
    <textarea v-model="t2" cols="30" rows="10" placeholder="文本2"></textarea>

    <h3>对比结果</h3>
    <div class="result">
        <div class="t1">
            <span v-for="item in t1_dom.split('')">{{ item }}</span>
        </div>
        <div class="t2" v-html="t2_dom"></div>
    </div>
</div>

<script>
    const { createApp } = Vue

    createApp({
        data() {
            return {
                t1: '',
                t2: ''
            }
        },
        computed: {
            t1_dom () {
                return this.t1.split("").filter(item => item.trim()).join("")
            },
            t2_dom () {
                var t1 = this.t1.split("").filter(item => item.trim()).join("")
                var t2 = this.t2.split("").filter(item => item.trim()).join("")
                var t2_result = []
                t1.split('').map((item, index) => {
                    if (t2[index] === item) {
                        t2_result.push(`<span>${t2[index]}</span>`)
                    } else {
                        t2_result.push(`<span class="err">${t2[index] || '-'}</span>`)
                    }
                })
                if (t2.length > t1.length) {
                    var other = t2.substring(t1.length)
                    t2_result.push("<span class='err'>" + other + "</span>")
                }
                return t2_result.join("")
            }
        }
    }).mount('#app')
</script>
</body>
</html>
